#include "StdAfx.h"
#include <iostream>
#include "StopConsonantAnalyzer.h"
#include "Statistics.h"

StopConsonantAnalyzer::StopConsonantAnalyzer(void)
{
}

StopConsonantAnalyzer::~StopConsonantAnalyzer(void)
{
}

void StopConsonantAnalyzer::MarkStopConsonants(short* sample, unsigned int length, std::vector<SampleMarker>& markers)
{
	int sliceSize = 1024;
	std::vector<Statistics> receiver;
	Statistics::GetStatistics(sample, length, receiver, sliceSize);

	SampleMarker::MarkerType type = SampleMarker::PostStopConsonant;

	std::vector<SampleMarker> curPreStopMarkers;
	for(unsigned int i=0; i<receiver.size(); i++)
	{
		Statistics stats = receiver[i];
		//std::cout << "Length: " << stats.GetLength() << ", Average: " << stats.GetAverage() << ", Max: " << stats.GetMax() << ", StdDev: " << stats.GetStdDeviation() << std::endl;

		unsigned int markerIdx = i*sliceSize;
		unsigned int markerEndIdx = markerIdx+sliceSize-1;
		if(stats.GetMax()<1000)
		{
			if(type==SampleMarker::StopConsonant)
			{
				curPreStopMarkers.clear();
			}
			else if(type==SampleMarker::PostStopConsonant)
			{
				curPreStopMarkers.clear();
				type = SampleMarker::PreStopConsonant;
			}
			else if(type==SampleMarker::PreStopConsonant)
			{
				//only mark when the second one and later come in
				SampleMarker preStopMarker(markerIdx, markerEndIdx, type);
				curPreStopMarkers.push_back(preStopMarker);
			}			
		}
		else if(stats.GetMax()>=1000)
		{
			if(type==SampleMarker::PreStopConsonant)
			{
				if(curPreStopMarkers.size()>1 && curPreStopMarkers.size()<10)
				{
					//we might very well have a stop consonant
					type = SampleMarker::StopConsonant;
					SampleMarker stopMarker(markerIdx, markerEndIdx, type);
					curPreStopMarkers.push_back(stopMarker);
				}
				else
				{					
					curPreStopMarkers.clear();
					type = SampleMarker::PostStopConsonant;
				}

			}
			else if(type==SampleMarker::StopConsonant)
			{
				for(unsigned int j=0; j<curPreStopMarkers.size(); j++)
				{
					markers.push_back(curPreStopMarkers[j]);
				}
				curPreStopMarkers.clear();
				type = SampleMarker::PostStopConsonant;				
				SampleMarker postMarker(markerIdx, markerEndIdx, type);
				markers.push_back(postMarker);
			}
		}
	}
	std::cout << markers.size();
	
}
